package com.myxy.mylogin;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initview2();//去注册
        toLogin();
    }

    private  void toLogin(){
        EditText admin = findViewById(R.id.edit_text);//账号
        EditText pswd = findViewById(R.id.edit_pswd);//密码
        Button btnLogin = findViewById(R.id.butt);//登录按钮
        btnLogin.setOnClickListener(new View.OnClickListener() {

            SharedPreferences sp = getSharedPreferences("app_login", Context.MODE_PRIVATE);
            //获取SharedPreference内的数据时，需要指定默认值
            String userAdmin = sp.getString("user_admin","没有该手机号");
            String userPswd = sp.getString("user_pwd","没有密码");

            @Override
            public void onClick(View v){
                if(TextUtils.isEmpty(admin.getText())||TextUtils.isEmpty(pswd.getText())){
                    //气泡提示
                    Toast.makeText(LoginActivity.this,
                            "账号或密码不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!userAdmin.equals(admin.getText().toString())){
                    Toast.makeText(LoginActivity.this,
                            "账号错误",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!userPswd.equals(pswd.getText().toString())){
                    Toast.makeText(LoginActivity.this,
                            "密码错误",Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(LoginActivity.this,
                        "登录成功",Toast.LENGTH_SHORT).show();

                SharedPreferences.Editor editor = sp.edit();
                editor.putString("user_type","0");//登录状态
                //3.提交到SharedPreferences文件
                //apply();为异步提交，不影响主线程
                editor.apply();

                //创建Intent
                Intent intent = new Intent(
                        LoginActivity.this, MainActivity.class);
                //跳转Activity需要使用Activity的方法为startActivity()
                //startActivity()方法需要一个Intent对象作为参数
                //并且，我们需要设置跳转的目的地为ChildActivity
                startActivity(intent);
                finish();
            }
        });
    }


    private void initview2(){
        TextView tozhuce = findViewById(R.id.zhuce);
        tozhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //创建Intent
                Intent intent = new Intent(
                        LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

    }
}